package data;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class Counter extends AbstractData {

	private long id = -1;
	private long hotelId;
	private String title;
	private String description;
	private boolean status;
	private boolean avaliable;
	private User lastUser;
	private String lastUserString;
	private boolean isOpen;
	
	public Counter()
	{
		status = true;
		avaliable = true;
	}
	
	public Counter(long id)
	{
		this.id = id;
		status = true;
		avaliable = true;
	}

	public void setId(long id) {
		this.id = id;
	}

	@Override
	public long getId() {
		return id;
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public boolean isStatus() {
		return status;
	}
	public void setStatus(boolean status) {
		this.status = status;
	}
	public boolean isAvaliable() {
		return avaliable;
	}
	public void setAvaliable(boolean avaliable) {
		this.avaliable = avaliable;
	}
	
	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}
	
	public User getLastUser() {
		if (lastUser == null && !(lastUserString == null || lastUserString.isEmpty())) {
			User c = new User(Long.parseLong(lastUserString));	
			c.LoadData();
			lastUser = c;
		}
		return lastUser;
	}

	public void setLastUser(User lastUser) {
		this.lastUserString = String.format("%d", lastUser.getId());
		this.lastUser = lastUser;
	}

	public String getLastUserString() {
		return lastUserString;
	}

	public void setLastUserString(String lastUserString) {
		this.lastUserString = lastUserString;
	}

	
	public boolean isOpen() {
		return isOpen;
	}

	public void setOpen(boolean isOpen) {
		this.isOpen = isOpen;
	}

	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.Counter);
		else
			item = new Entity(this.key);

		item.setProperty("hotelId", this.hotelId);
		item.setProperty("title", this.title);
		item.setProperty("description", this.description);
		item.setProperty("status", this.status);
		item.setProperty("avaliable", this.avaliable);
		item.setProperty("lastUser", this.lastUserString);
		item.setProperty("isOpen", this.isOpen);
		return item;
	}
	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.title = (String) entity.getProperty("title");
		this.description = (String) entity.getProperty("description");
		this.status = (Boolean) entity.getProperty("status");
		this.avaliable = (Boolean) entity.getProperty("avaliable");
		this.isOpen = (Boolean) entity.getProperty("isOpen");
		this.lastUserString = (String) entity.getProperty("lastUser");
		this.key = entity.getKey();
	}

	
	public void LoadData()
	{
		if(this.id > -1)
		{
			Entity entity = LoadData(EntityName.Counter,this.id);
			setEntity(entity);
		}
	}
}
